﻿Imports Persistencia
Public Class ctrCliente
    Inherits ctrPadrao

#Region "Enumerações"

    Public Enum eMensagens As Byte
        NOME_OBRIGATORIO = 0
    End Enum

#End Region

#Region "Declaração de variáveis"

    Private objCliente As New perCliente
    Private objStatus As New ctrStatus

    Private sMensagens() As String = {"Razão social obrigatória. "}

    Private iIdCliente As Int32

#End Region

#Region "Propriedades"

    Public Property IDCliente() As Int32
        Get
            Return Me.iIdCliente
        End Get
        Set(ByVal value As Int32)
            Me.iIdCliente = value
        End Set
    End Property
    Public ReadOnly Property SqlConsulta()
        Get
            Return Me.objCliente.sqlConsulta()
        End Get
    End Property
    Public Function selecionarCliente(ByVal iIDCliente As Int32) As DataRow
        Try
            Return Me.objCliente.selecionarCliente(iIDCliente)
        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarCliente'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try
    End Function

#End Region

#Region "Métodos"

    Public Function salvar(ByVal iIdCliente As Int32, _
                          ByVal sRazaoSocial As String, _
                          ByVal sNomeFantasia As String, _
                          ByVal sEndereco As String, _
                          ByVal sTelefone As String, _
                          ByVal sEmail As String, _
                          ByVal sCPF As String, _
                          ByVal iIdBairro As Int32, _
                          ByVal iIdCidade As Int32, _
                          ByVal iIdEstado As Int32, _
                          ByVal sCEP As String, _
                          ByVal iIdStatus As Int32, _
                          ByVal iTpPessoa As Int32, _
                          ByVal sCNPJ As String, _
                          ByVal sInsc As String, _
                          ByVal sObservacoes As String, _
                          ByVal iIdRegiao As Int32, _
                          ByVal iIdUsuario As Int32, _
                          ByVal dtProxContato As Date, _
                          ByVal iIdRamoAtividade As Int32) As Boolean

        Dim bResultado As Boolean = False

        If Me.validarDados(sRazaoSocial) Then

            If iIdCliente > 0 Then
                Me.IDCliente = iIdCliente
                Me.alterarPessoa(sRazaoSocial, _
                                  sNomeFantasia, _
                                  sEndereco, _
                                  sTelefone, _
                                  sEmail, _
                                  sCPF, _
                                  iIdBairro, _
                                  iIdCidade, _
                                  iIdEstado, _
                                  sCEP, _
                                  iIdStatus, _
                                  iTpPessoa, _
                                  sCNPJ, _
                                  sInsc, _
                                  sObservacoes, _
                                  iIdRegiao, _
                                  iIdUsuario, _
                                  dtProxContato)

                bResultado = True
            Else
                Me.iIdCliente = Me.inserirCliente(sRazaoSocial, _
                                                  sNomeFantasia, _
                                                  sEndereco, _
                                                  sTelefone, _
                                                  sEmail, _
                                                  sCPF, _
                                                  iIdBairro, _
                                                  iIdCidade, _
                                                  iIdEstado, _
                                                  sCEP, _
                                                  iIdStatus, _
                                                  iTpPessoa, _
                                                  sCNPJ, _
                                                  sInsc, _
                                                  sObservacoes, _
                                                  iIdRegiao, _
                                                  iIdUsuario, _
                                                  dtProxContato)

                bResultado = True
            End If

        Else
            bResultado = False
        End If

        Return bResultado

    End Function

    Public Sub excluirPessoa(ByVal iIdPessoa As Int32)

        Try
            Me.objCliente.excluirPessoa(iIdPessoa)
        Catch ex As Exception
            Throw New Exception("Exceção no método 'excluirPessoa'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Private Function validarDados(ByVal sNome As String) As Boolean

        Dim bRetorno As Boolean = True

        Try

            If sNome = String.Empty Then
                MyBase.adicionarMensagensValidacao(eMensagens.NOME_OBRIGATORIO, Me.sMensagens(eMensagens.NOME_OBRIGATORIO))
                bRetorno = False
            End If

            Return bRetorno

        Catch ex As Exception
            Throw New Exception("Exceção no método 'validarDados'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Private Sub alterarPessoa(ByVal sRazaoSocial As String, _
                              ByVal sNomeFantasia As String, _
                              ByVal sEndereco As String, _
                              ByVal sTelefone As String, _
                              ByVal sEmail As String, _
                              ByVal sCPF As String, _
                              ByVal iIdBairro As Int32, _
                              ByVal iIdCidade As Int32, _
                              ByVal iIdEstado As Int32, _
                              ByVal sCEP As String, _
                              ByVal iIdStatus As Int32, _
                              ByVal iTpPessoa As Int32, _
                              ByVal sCNPJ As String, _
                              ByVal sInsc As String, _
                              ByVal sObservacoes As String, _
                              ByVal iIdRegiao As Int32, _
                              ByVal iIdUsuario As Int32, _
                              ByVal dtProxContato As Date)
        Try
            Me.objCliente.AlterarCliente(Me.IDCliente, _
                                       sRazaoSocial, _
                                       sNomeFantasia, _
                                       sEndereco, _
                                       sTelefone, _
                                       sEmail, _
                                       sCPF, _
                                       iIdBairro, _
                                       iIdCidade, _
                                       iIdEstado, _
                                       sCEP, _
                                       iIdStatus, _
                                       iTpPessoa, _
                                       sCNPJ, _
                                       sInsc, _
                                       sObservacoes, _
                                       iIdRegiao, _
                                       iIdUsuario, _
                                       dtProxContato)
        Catch ex As Exception
            Throw New Exception("Exceção no método 'alterarPessoa'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub
    Public Function AtualizaProximoContato(ByVal iIDCliente As Int32, ByVal iIDStatus As Int32, ByVal dtContato As Date) As Boolean
        Dim bRetorno As Boolean
        Dim dtProxContato As Date

        Try
            dtProxContato = objStatus.GeraProximoContato(iIDStatus, dtContato).Date
            If (dtProxContato <> Date.MinValue) And _
               (objCliente.AtualizaDataProximoContato(iIDCliente, dtProxContato)) Then
                bRetorno = True
            Else
                MsgBox("Data próximo contato não atualizada.", MsgBoxStyle.Exclamation, "Login")
            End If

        Catch ex As Exception

            Throw New Exception("Exceção no método 'AtualizaProximoContato'." & Environment.NewLine & Environment.NewLine & ex.Message)
            bRetorno = False

        End Try

        Return bRetorno

    End Function
    Private Function inserirCliente(ByVal sRazaoSocial As String, _
                                  ByVal sNomeFantasia As String, _
                                  ByVal sEndereco As String, _
                                  ByVal sTelefone As String, _
                                  ByVal sEmail As String, _
                                  ByVal sCPF As String, _
                                  ByVal iIdBairro As Int32, _
                                  ByVal iIdCidade As Int32, _
                                  ByVal iIdEstado As Int32, _
                                  ByVal sCEP As String, _
                                  ByVal iIdStatus As Int32, _
                                  ByVal iTpPessoa As Int32, _
                                  ByVal sCNPJ As String, _
                                  ByVal sInsc As String, _
                                  ByVal sObservacoes As String, _
                                  ByVal iIdRegiao As Int32, _
                                  ByVal iIdUsuario As Int32, _
                                  ByVal dtProxContato As Date)
        Dim iIdCliente As Int32

        Try
            iIdCliente = Me.objCliente.inserirCliente(Trim(sRazaoSocial), _
                                                       Trim(sNomeFantasia), _
                                                       Trim(sEndereco), _
                                                       Trim(sTelefone), _
                                                       Trim(sEmail), _
                                                       Trim(sCPF), _
                                                       iIdBairro, _
                                                       iIdCidade, _
                                                       iIdEstado, _
                                                       Trim(sCEP), _
                                                       iIdStatus, _
                                                       iTpPessoa, _
                                                       Trim(sCNPJ), _
                                                       Trim(sInsc), _
                                                       Trim(sObservacoes), _
                                                       iIdRegiao, _
                                                       iIdUsuario, _
                                                       dtProxContato)
            Return iIdCliente

        Catch ex As Exception
            Throw New Exception("Exceção no método 'inserirCliente'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Function RetornarClientesProxDataContato(ByVal dtBase As Date, ByVal bSomenteAtrasados As Boolean, ByVal iIDStatus As Int32) As DataTable

        Try
            Return objCliente.RetornarClientesPorDataContato(dtBase, bSomenteAtrasados, 0, iIDStatus, 0)
        Catch ex As Exception
            Throw New Exception("Exceção no método 'RetornarClientesProxDataContato'." & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

#End Region

End Class
